# 第一题
def greet(name="张三", age=20, city="北京"):
    print(f"我是{name}，今年{age}岁，来自{city}。")

greet()
greet("李四")  # 只传递name
greet("王五", 25)  # 传递name和age
greet("赵六", 30, "上海")  # 传递所有参数
# 第二题
# 元组拆包
def greet_tuple(*args):
    print(f"你好，{'，'.join(args)}！")

greet_tuple("张三", "李四", "王五")

# 字典拆包
def greet_dict(**kwargs):
    for key, value in kwargs.items():
        print(f"{key}: {value}")

greet_dict(name="张三", age=20, city="北京")
# 第三题
class Dog:
    def __init__(self, color, name):
        self.color = color
        self.name = name

    def bark(self):
        print(f"{self.name}：汪汪叫！")

    def wag_tail(self):
        print(f"{self.name}：摇尾巴！")


dog = Dog("黄", "小黄")
dog.bark()  # 汪汪叫
dog.wag_tail()  # 摇尾巴
# 第四题
class BankAccount:
    def __init__(self, balance=0):
        self.__balance = balance  # 私有属性

    def deposit(self, amount):
        if amount > 0:
            self.__balance += amount
            print(f"存入{amount}元，当前余额：{self.__balance}元")
        else:
            print("存入金额必须大于零")

    def withdraw(self, amount):
        if 0 < amount <= self.__balance:
            self.__balance -= amount
            print(f"取出{amount}元，当前余额：{self.__balance}元")
        else:
            print("余额不足或金额不合法")

    def get_balance(self):
        return self.__balance  # 通过公有方法访问私有属性


# 实例化银行账户并进行操作
account = BankAccount(1000)
account.deposit(500)
account.withdraw(300)
print("最终余额:", account.get_balance())
# 第五题
class Dog:
    def __init__(self, name):
        self.name = name
        self.__age = 5  # 私有属性

    def __private_method(self):  # 私有方法
        print("这是一个私有方法！")

    def public_method(self):
        print(f"狗狗{self.name}，年龄{self.__age}")
        self.__private_method()  # 在公有方法中调用私有方法

# 创建实例
dog = Dog("小黄")
dog.public_method()  # 可以通过公有方法访问
# dog.__private_method()  # 不能直接访问私有方法
# 第六题
# 单继承
class Animal:
    def sound(self):
        print("动物的声音")

class Dog(Animal):
    def sound(self):
        print("汪汪叫！")

dog = Dog()
dog.sound()  # 输出：汪汪叫！

# 多重继承
class A:
    def method_a(self):
        print("来自类A的方法")

class B:
    def method_b(self):
        print("来自类B的方法")

class C(A, B):
    def method_c(self):
        print("来自类C的方法")

c = C()
c.method_a()
c.method_b()
c.method_c()
# 第七题
class Singleton:
    _instance = None

    def __new__(cls):
        if cls._instance is None:
            cls._instance = super(Singleton, cls).__new__(cls)
        return cls._instance

# 测试单例模式
s1 = Singleton()
s2 = Singleton()

print(s1 is s2)  # 输出True，确保两个实例是同一个
